

local damage_wds = {} 
local accumulated_damage = 0  -- 累积伤害
local damage_window = 5
AddComponentPostInit("health", function(Health)
    local oldDoFireDamage = Health.DoFireDamage
    function Health:DoFireDamage(amount, doer, instant, ...)
        if self.currenthealth > 0 and self.inst.components.burnable and self.inst.components.burnable.i26laotie then
            self.currenthealth = math.max(self.currenthealth - (self.maxhealth/1000), .01) --防卡
        end
        return oldDoFireDamage(self, amount, doer, instant, ...)
    end

    if not Health.inst:HasTag("i26") then return end 

    local yestody = Health.maxhealth
    local last_dam = 0

    function Health:I26Down(oldhp, hp, lim)
        local num = oldhp - hp
        local t = GetTime()
        
        table.insert(damage_wds, {
            t = t,
            val = num,
        })
        accumulated_damage = accumulated_damage + num
        
        for i = #damage_wds, 1, -1 do
            local v = damage_wds[i]
            if t - v.t > damage_window then
                accumulated_damage = accumulated_damage - v.val
                table.remove(damage_wds, i)
            end
        end

        if accumulated_damage > lim then
            hp = hp + math.clamp(accumulated_damage - lim, .1, num)
        end
        return hp
    end

    function Health:SetVal(val, cause, afflicter, ...)
        local old_health = yestody
        local max_health = self:GetMaxWithPenalty()
        local min_health = math.min(self.minhealth or 0, max_health)

        if val < old_health and self.i26limi then
            local dancixs = max_health / 20
            if old_health - val >= dancixs then
                val = old_health - dancixs
            end
            val = self:I26Down(old_health, val, max_health / 2)
        end

        if val > max_health then
            val = max_health
        end
    
        if val <= min_health then
            self.currenthealth = min_health
            yestody = min_health
            self.inst:PushEvent("minhealth", { cause = cause, afflicter = afflicter })
        else
            self.currenthealth = val
            yestody = val
        end
    
        if old_health > 0 and self.currenthealth <= 0 then
            TheWorld:PushEvent("entity_death", { inst = self.inst, cause = cause, afflicter = afflicter })
            self.inst:PushEvent("death", { cause = cause, afflicter = afflicter })
    
            local notify_type = (self.inst.isplayer and "TotalPlayersKilled") or "TotalEnemiesKilled"
            NotifyPlayerProgress(notify_type, 1, afflicter)
    
            if not self.nofadeout then
                self.inst:AddTag("NOCLICK")
                self.inst.persists = false
                self.inst.erode_task = self.inst:DoTaskInTime(self.destroytime or 2, ErodeAway)
            end
        end
    end

    local oldSetMaxHealth = Health.SetMaxHealth
    function Health:SetMaxHealth(amount, ...)
        yestody = amount
        return oldSetMaxHealth(self, amount, ...)
    end
    

    local oldDoDelta = Health.DoDelta
    function Health:DoDelta(amount, overtime, cause, ignore_invincible, afflicter, ignore_absorb, ...)
        if amount and amount < 0 then
            amount = amount * .7
        end
        return oldDoDelta(self, amount, overtime, cause, ignore_invincible, afflicter, ignore_absorb, ...)
    end

    local oldOnLoad = Health.OnLoad
    function Health:OnLoad(data, ...)
        if data and data.health then
            yestody = data.health
        end
        return oldOnLoad(self, data, ...)
    end
end)